package org.redis.cluster.admin.client.controller;

import java.util.Collection;

import org.redis.cluster.admin.Constants;
import org.redis.cluster.admin.client.adapter.SlotClientConfigAdapter;
import org.redis.cluster.admin.client.model.RoutingClientConfig;
import org.redis.cluster.admin.client.model.SlotClientConfig;
import org.redis.cluster.admin.client.service.ClientService;
import org.redis.cluster.admin.result.Result;
import org.redis.cluster.admin.result.ResultBuilder;
import org.redis.cluster.config.ClientConfig;
import org.redis.cluster.loadbalance.slot.SlotMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(Constants.API_ROOT+"/client")
public class ClientController {
	
	@Autowired
	private ClientService clientService;

	/**
	 * 返回
	 * @return
	 */
	@RequestMapping(value="",method=RequestMethod.GET)
	public Result getClients() {
		Collection<RoutingClientConfig> configs=clientService.getRoutingClients();
		return ResultBuilder.success(configs);
	}
	
	/**
	 * 注册
	 * @param config
	 * @return
	 */
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public Result register(SlotClientConfig config) {
		clientService.register(SlotClientConfigAdapter.adaptClientConfig(config));
		return ResultBuilder.success(config);
	}
	
	/**
	 * 取消注册
	 * @param id
	 * @param target
	 * @return
	 */
	@RequestMapping(value="/{id}/unregister",method=RequestMethod.POST)
	public Result unregister(@PathVariable String id,String target) {
		boolean result=clientService.unregister(id,target);
		
		if(result) {
			return ResultBuilder.success("取消成功");
		}
		else {
			return ResultBuilder.failed("取消失败");
		}
	}
	
	/**
	 * 注册
	 * @param id
	 * @param config
	 * @return
	 */
	@RequestMapping(value="/{sourceId}/routing/register",method=RequestMethod.POST)
	public Result registerRouting(@PathVariable String sourceId,ClientConfig config) {
		boolean result=clientService.registerRouting(sourceId, config);
		
		if(result) {
			return ResultBuilder.success("注册成功");
		}
		else {
			return ResultBuilder.failed("注册失败");
		}
	}
	
	/**
	 * 取消注册
	 * @param id
	 * @param routingId
	 * @return
	 */
	@RequestMapping(value="/{id}/routing/unregister",method=RequestMethod.POST)
	public Result unregisterRouting(@PathVariable String id,String routingId) {
		boolean result=clientService.unregisterRouting(id, routingId);
		
		if(result) {
			return ResultBuilder.success("取消成功");
		}
		else {
			return ResultBuilder.failed("取消失败");
		}
	}
	
	/**
	 * info
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/{id}/info",method=RequestMethod.GET)
	public Result getInfo(@PathVariable String id) {
		String result=clientService.getInfo(id);
		if(result==null) {
			return ResultBuilder.failed("返回错误");
		}
		
		return ResultBuilder.success(result);
	}
	
	/**
	 * mapping
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/{id}/mapping",method=RequestMethod.GET)
	public Result getSlotMapping(@PathVariable String id) {
		SlotMapping slotMapping=clientService.getSlotMapping(id);
		return ResultBuilder.success(slotMapping);
	}
}